## This is R code for "The Demand for Insurance: Incorporating the Severity of
## Losing Office into the Insurance Model of Judicial Independence"
## Robustness check
## Appendix: using alternative measures of threat which prioritize the likelihood
## of losing office rather than treat it (along with risks associated)
## as an equal component of the product that is the threat measure

rm(list = ls())
## install.packages("pacman") #load all packages
## remotes::install_github("chrisadolph/simcf") since simcf cannot be directly installed
pacman::p_load(
  tidyverse,this.path,texreg,MASS,AER,ivregEX,ivpack,simcf,arm,stargazer,
  ivmodel,modelsummary,xtable
)
## set working directory
setwd(this.path::here(..=1))
## read data
load("Data/dat.RData")
## create directory to save the results for these alternative measures
robustness_dir <- "./RobustnessCheck/alternativemeasures/"
dir.create(robustness_dir, showWarnings = FALSE)


## 1. Preparation -----------------------------------------------------------------
## Creat alternative measures
threathold1 <- 0.4 #mean(dat$e_h_polcon3_lag1, na.rm=T)
threathold2 <- quantile(dat$e_h_polcon3_lag1, .25, na.rm=T)

dat <- dat %>%
  mutate(
    threat = threat_cumsum,
    threatS = threatS_cumsum,
    ## alternative measure 1 using the square of the risk of losing office/competition
    polcon10_lag1 = e_h_polcon3_lag1*10,
    threat_squared = (punish_percent_cumsum +1)*polcon10_lag1^2, 
    threat_squaredS = threat_squared/(2*sd(threat_squared,na.rm=TRUE)), 
    ## alternative measure 2 using a threathold set to the mean level of polcon (0.4)
    polcon_min40 = ifelse(e_h_polcon3_lag1 < threathold1, 0, e_h_polcon3_lag1), 
    threat_min40 = (punish_percent_cumsum +1)*polcon_min40, 
    threat_min40S = threat_min40/(2*sd(threat_min40,na.rm=TRUE)),
    ## alternative measure 3 using a threshold set to the first quartile level of polcon (0.3467)
    polcon_min1Q = ifelse(e_h_polcon3_lag1 < threathold2, 0,  e_h_polcon3_lag1),
    threat_min1Q = (punish_percent_cumsum +1)*polcon_min1Q,
    threat_min1QS = threat_min1Q/(2*sd(threat_min1Q,na.rm=TRUE)),
    aid_crsc_lag1=ifelse(year>1962&is.na(aid_crsc_lag1), 0, aid_crsc_lag1),
    aid_crsio_lag1=ifelse(is.na(aid_crsio_lag1), 0, aid_crsio_lag1),
    totalaid_lag1=aid_crsc_lag1+aid_crsio_lag1,
    ivvar1 = log((ross_oil_value_2000_lag1 + 1) * (neighbormean_punish_percent_cumsum + 1)+1),
    ivvar2 = log((totalaid_lag1) * (neighbormean_punish_percent_cumsum +1)+1)
  ) 


## 2. Correlations between alternative measures of demand ----------------------
myData <- dat %>% dplyr::select(c(threat,threat_squared,threat_min40, threat_min1Q))
res <- cor(myData,use="pairwise.complete.obs")
round(res,2)
colnames(res) <- c("Original demand","Competition squared","Competition > 0.40", "Competition > 0.347")
rownames(res) <- c("Original demand","Competition squared","Competition > 0.40", "Competition > 0.347")

xtable(res,
       caption="\\textbf{Correlations between alternative measures of demand.} 
       Correlation matrix of original measure of demand for insurance and three alternative specifications",
       label="table:correlations")

## 3. Run all models for the alternative measures  -----------------------------
## Alternative measure 1 -------------------------------------------------------
## This uses the square of the risk of losing office/competition
dat <- dat %>%
  mutate(threatS=threat_squaredS)

## Specify the directories and captions for table 
dir_table <- paste0(robustness_dir, "appendix_alternative_threat1.tex")
texcaption <- "\\textbf{Alternative measures of demand.} Models of de facto independence using alternative demands for independence. Models 1--4 use the product of the square of the risks of losing office (electoral competition) and the risks associated with losing office (percent previous leaders punished)."
texlabel <- "table:alt_threat1"
source("Code/EstimationTableOnly.R")

## Alternative measure 2 -------------------------------------------------------
## This uses a threshold for the risk of losing office/competition, where
## the threshold is set to the mean level of polcon (0.4)
dat <- dat %>%
  mutate(threatS=threat_min40S)

## Specify the directories and captions for table 
dir_table <- paste0(robustness_dir, "appendix_alternative_threat2.tex")
texcaption <- "\\textbf{Alternative measures of demand.} Models of de facto independence using alternative demands for independence. Models 1--4 use the product of the risks of losing office (electoral competition) and the risks associated with losing office (percent previous leaders punished) using a threshold for competition. Here, all instances of competition lower than the mean level (0.4) are set to zero. "
texlabel <- "table:alt_threat2"
source("Code/EstimationTableOnly.R")

## Alternative measure 3 -------------------------------------------------------
## This uses a threshold for the risk of losing office/competition, where
## the threshold is set to the first quartile level of polcon (0.3467)
dat <- dat %>%
  mutate(threatS=threat_min1QS)

## Specify the directories and captions for table 
dir_table <- paste0(robustness_dir, "appendix_alternative_threat3.tex")
texcaption <- "\\textbf{Alternative measures of demand.} Models of de facto independence using alternative demands for independence. Models 1--4 use the product of the risks of losing office (electoral competition) and the risks associated with losing office (percent previous leaders punished) using a threshold for competition. Here, all instances of competition lower than the first quartile level (0.347) are set to zero."
texlabel <- "table:alt_threat3"
source("Code/EstimationTableOnly.R")


## Additional notes
## To get the AIC/BIC manually run the following code after running the code for 
## each alternative measures
## modelsummary(fe1, vcov= clustered_vcov, stars=TRUE) 
## modelsummary(fe2, vcov= clustered_vcov2, stars=TRUE) 
## modelsummary(iv_total1, vcov= clustered_vcov_ivtotal1, stars=TRUE) 
## modelsummary(iv_total2, vcov= clustered_vcov_ivtotal2, stars=TRUE) 	

## Alternative measure 1 results

# | AIC                                  | -4747.1   |
# +--------------------------------------+-----------+
# | BIC                                  | -4233.8   |	

# | AIC                                  | -5555.0   |
# +--------------------------------------+-----------+
# | BIC                                  | -5030.2   |	

# | AIC             | -169.9    |
# +-----------------+-----------+
# | BIC             | -142.3    |

# | AIC             | 49.7      |
# +-----------------+-----------+
# | BIC             | 77.2      |	

## Alternative measure 2 results

# | AIC                                  | -4706.4   |
# +--------------------------------------+-----------+
# | BIC                                  | -4193.0   |

# | AIC                                  | -5527.9   |
# +--------------------------------------+-----------+
# | BIC                                  | -5003.2   |

# | AIC           | 376.5     |
# +---------------+-----------+
# | BIC           | 404.1     |
	
# | AIC           | 449.0     |
# +---------------+-----------+
# | BIC           | 476.4     |

## Alternative measure 3 results
## need to get AIC/BIC manually

# | AIC                                  | -4727.3   |
# +--------------------------------------+-----------+
# | BIC                                  | -4213.9   |
# +--------------------------------------+-----------+

# | AIC                                  | -5551.2   |
# +--------------------------------------+-----------+
# | BIC                                  | -5026.5   |

# | AIC           | 1683.0    |
# +---------------+-----------+
# | BIC           | 1710.6    |

# | AIC           | 1188.8    |
# +---------------+-----------+
# | BIC           | 1216.3    |	